import java.math.*;

import java.util.Vector;



public  abstract class Cromosoma implements Cloneable {
		
	private int[] genoma;
	
	private double fenotipo;
	
	private double aptitud;
	
	private  double puntuacion;
	
	private double puntuacion_acumulada;
	
	private double min;

	private double max;
	
	private double tolerancia;
	
	private int primeraVar;
	
	private  int n;
	
	private boolean maximiza;
	
	nodoArbol cromosoma;
	
	
	public double getAptitud () {return aptitud;}
	
	public void setAptitud (double apt) {aptitud = apt;}

	
	public boolean getMaximiza () {return maximiza;}
	

	public void  setMaximiza (boolean maxim) {maximiza = maxim;}
	
	public void setMax (double m) {max=m;}
	
	
	public void setMin (double m) {min=m;}

	public void setN (int m) {n=m;}
	
	public int getN () {return n;}
	
	public void setPuntuacion (double punt) {puntuacion=punt;}
	
	public double getPuntuacion () {return puntuacion;}
	
public void setPuntuacionAcu (double punt) {puntuacion_acumulada=punt;}
	
	public double  getPuntuacionAcu () {return puntuacion_acumulada;}
	
public void setPrimeraVar (int prVar) {primeraVar=prVar;}
	
	public int getPrimeraVar () {return primeraVar;}
	
	public void setToler (double toler) {tolerancia=toler;}
	
	public double getToler () {return tolerancia;}




public Cromosoma () {
		
	}	

public int calculaTamGenoma (){
	double log;
	int tam;
	
	log = Math.log(1 + ((max-min)/tolerancia));
	
	log = log/Math.log(2); 
	
	tam = (int) Math.ceil(log);
	
	return tam;
	}


	
public Cromosoma (int tam) {
	
	maximiza=true;
	
	genoma = new int[tam];
}

public int[] getGenoma () {return genoma;}

public int getPosGenoma (int pos) {return genoma[pos];}

public void setGenoma (int[] gen) {genoma = gen;}

public void setPosGenoma (int pos, int valor){genoma[pos] = valor;}

public double getMin () {return min;}

public double getMax () {return max;}

public void inicializaCromosoma(int funcion) {
	
	
		for (int i = 0; i <genoma.length; i++) {
			double random = Math.random();
			
			if (random>0.5)
				genoma[i]= 1;
			else
				genoma[i]=0;	
		}
	}



//funcion que traduce el valor binario a un valor real;

public double  fenotipo () {
	
		
	double valor=min;
	
	//DE LA TEORIA x(v) = bin2dec(v) * (max-min/2^lcrom -1)
	
	valor = min +  bintodec(genoma) *((max-min)/(Math.pow(2, this.genoma.length)-1));
	
	

	
	return valor;}

public int bintodec (int[] cadenaBool){
	
	int valor=0; 
	for (int cont=0;cont<cadenaBool.length;cont++){
		
		valor = valor + (cadenaBool[cont]*(int)Math.pow(2, cont));
		
	}
	
	return valor;
}


//funcion que hace f(x) dado el fenotipo (CALCULA EL FITNESS)

public abstract double evalua ();

public int aleatorio (int max) {
	
	double aleat = Math.random() * 10000;
	int aleatorio = (int) Math.floor(aleat);
	aleatorio = aleatorio % max;
	return aleatorio;
}

public Object clone() throws CloneNotSupportedException {
    return super.clone();
  }

}